<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/11/15
 * Time: 11:13
 */

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Auth;
use Illuminate\Support\Facades\View;
use Session;
use Request;
use App\Services\Upload;
use Validator;
use Redirect;
class BlogController extends Controller
{
    public function add(){

        if(!Session::get("user_info")){
            $url = url('/Log');
            return Redirect::to($url);
        }

        $id = Request::input("id");
        $data['nav'] = 'blog';
        $data['handleUrl'] = "addhandle";
        $data['blogInfo'] = '';
        $data['categories'] = Model("Category")->get();

        if($id){
            $blog_m = Model("blog");
            $data['blogInfo'] = $blog_m->blogInfo($id);
            $data['handleUrl'] = "addhandle?id={$id}";
        }

        return view('admin.blog.index',$data);
    }

    private function blog_init(){
        $data['title']        = Request::input("title");
        $data['summary']      = Request::input("summary");
        $data['content']      = Request::input("content");
        $data['main_img']     = Request::input("main_img");
        $data['category']     = Request::input("category");
        $data['tags']     	  = Request::input("tags");
        $data['thumbnailUrl'] = "";
        $data['thumbnailUrl'] = '';
        $data['main_img'] = 0;
        $data['user_id'] = Session::get('user_info')['id'];

        $img_id = Request::input("cate_img");

        if($img_id >0){
            $data['main_img'] = $img_id;
        }else{
            $up = Upload::input2Res("main_img","upload/blog");
            if($up){
                $data['main_img'] = $up->id;
            }
            if($data['thumbnailUrl']){
                $data['thumbnailUrl'] = Request::input("thumbnailUrl");
            }
        }



        return $data;
    }


    public function addhandle(){

        $sta = 1000;
        $data = $this->blog_init();

        $id = Request::input("id");


        if($id){
            if($data['main_img'] == 0){
                unset($data['main_img']);
            }
            $data['id']      = $id;
        }

        $blog_m = Model("Blog");

        $invalid = $this->blogCheck($data);

        if($invalid){

            $json_data['invalid'] = $invalid;

            return $this->showMsg(-2001);
        }

        $rs = $blog_m->blog_save($data);


        if(!$rs){
            $sta = -1000;
        }
        return $this->showMsg($sta,'',"/Admin/Blog/bloglist");
    }


    private function blogCheck($data){
        $rule = [
            "title"   => "required|max:50",
            "summary" => "required|max:100",
            "content" => "required",
        ];

        $validator = Validator::make($data,$rule);

        if($validator->fails()){
            return json_decode(json_encode($validator->messages()),true);
        }

        return false;

    }

    public function bloglist(){
        if(!Session::get("user_info")){
            $url = url('/Log');
            return Redirect::to($url);
        }
        $blog_m = Model("Blog");

        $blogList = $blog_m->getBlogListByUser(15);
        $nav = 'list';
        $var = compact("blogList","nav");

        return view('admin.blog.bloglist', $var);
    }

    public function delete(){
        if(!Session::get("user_info")){
            $url = url('/log');
            return Redirect::to($url);
        }
        if(!Request::input('id')){
            return $this->showMsg(-2001);
        }

        $id = (int)Request::input('id');

        $blog_m = Model("blog");

        $blogInfo = $blog_m->blogInfo($id);

        if(!$blogInfo){
            return $this->showMsg(-2002);
        }

        $rs = $blog_m->remove($id);

        if($rs){
            return $this->showMsg(2001,'',"/Admin/Blog/bloglist");
        }else{
            return $this->showMsg(-2003);
        }

    }

    public function addCate(){
        $cate = trim(Request::input("_cate"));

        $cate_m = Model('Category');
        $isExisted = $cate_m->where(['name' => $cate])->first();
        if($isExisted){
            return $this->send(-20,"同名分类已经存在");
        }
        $cate_m->fill(["name"=>$cate,"order"=>0])->save();
        if(!$cate_m){
            return $this->send(-1,"出错了");
        }
        $data = $cate_m->toArray();
        return $this->send(1,$data);
    }

    public function isSee(){
        if(!Session::get("user_info")){
            return $this->send(-99,'请先登录！');
        }
        if(!Request::input('id')){
            return $this->send(-3,'请选择文章！');
        }

        $id = (int)Request::input('id');
        $is_show = (int)Request::input('is_show');

        $blog_m = Model("blog");

        $blogInfo = $blog_m->blogInfo($id);
        $data = [
            'id' => $id,
            'is_show' => $is_show,
        ];
        if(!$blogInfo){
            return $this->send(-1,'文章不存在！');
        }


        if($blogInfo->user_id != Session::get("user_info")['id']){
            return $this->send(-2,'您没有权限修改！');
        }

        $res = $blog_m->isSee($data);

        if($res){
            return $this->send(1,'修改成功！');
        }else{
            return $this->send(0,'修改失败！');
        }

    }
}